package com.movie.controller;

import com.movie.dto.ApiResponse;
import com.movie.dto.TagDTO;
import com.movie.dto.PageResponse;
import com.movie.service.TagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tags")
@RequiredArgsConstructor
@Tag(name = "标签管理", description = "影片标签的增删改查接口")
public class TagController {
    
    private final com.movie.service.impl.TagServiceImpl tagService;
    
    @GetMapping
    @Operation(summary = "获取所有标签", description = "获取系统中的所有标签列表")
    public ApiResponse<List<TagDTO>> getAllTags() {
        List<TagDTO> tags = tagService.findAll();
        return ApiResponse.success(tags);
    }
    
    @GetMapping("/hot")
    @Operation(summary = "获取热门标签", description = "获取使用频率最高的热门标签")
    public ApiResponse<List<TagDTO>> getHotTags() {
        List<TagDTO> tags = tagService.findHotTags();
        return ApiResponse.success(tags);
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取标签", description = "根据标签ID获取具体的标签信息")
    public ApiResponse<TagDTO> getTagById(
            @Parameter(description = "标签ID") @PathVariable Long id) {
        TagDTO tag = tagService.findById(id);
        return ApiResponse.success(tag);
    }
    
    @PostMapping
    @Operation(summary = "创建标签", description = "创建新的标签")
    public ApiResponse<TagDTO> createTag(
            @Parameter(description = "标签信息") @RequestBody TagDTO tagDTO) {
        TagDTO created = tagService.create(tagDTO);
        return ApiResponse.success("标签创建成功", created);
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新标签", description = "根据ID更新标签信息")
    public ApiResponse<TagDTO> updateTag(
            @Parameter(description = "标签ID") @PathVariable Long id,
            @Parameter(description = "标签信息") @RequestBody TagDTO tagDTO) {
        TagDTO updated = tagService.update(id, tagDTO);
        return ApiResponse.success("标签更新成功", updated);
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除标签", description = "根据ID删除标签")
    public ApiResponse<Void> deleteTag(
            @Parameter(description = "标签ID") @PathVariable Long id) {
        tagService.delete(id);
        return ApiResponse.success("标签删除成功", null);
    }
    
    @GetMapping("/stats")
    @Operation(summary = "获取标签统计", description = "获取标签使用统计信息")
    public ApiResponse<Map<String, Object>> getTagStats() {
        Map<String, Object> stats = tagService.getTagStatistics();
        return ApiResponse.success(stats);
    }
}